#include "splatter.h"
#include "input.h"
#include "footprint.h"
#include "color.h"

namespace Splatit {

Splatter::Splatter(void) {
}

Splatter::~Splatter(void) {

}

//1-direcao 'x'; 2- direcao 'y'; 3 direcao 'z'
unsigned int Splatter::parallel(){
	double cos;
	double smaller=1;
	unsigned int result;
	Vector grid_z(0.0,0.0,1.0);
	Vector u,v;

	
	u=volume->get_voxel_position(volume->width,0,0) - volume->get_voxel_position(0,0,0);
	v=volume->get_voxel_position(0,volume->height,0) - volume->get_voxel_position(0,0,0);
	cos = (u.cross_product(v).normalize()).dot_product(grid_z);
	if(cos<smaller){
		smaller=cos;
		result=3;
	}
	u=volume->get_voxel_position(0,volume->height,0) - volume->get_voxel_position(0,volume->height,volume->depth);
	v=volume->get_voxel_position(volume->width,volume->height,volume->depth) - volume->get_voxel_position(0,volume->height,volume->depth);
	cos = (u.cross_product(v).normalize()).dot_product(grid_z);
	if(cos<smaller){
		smaller=cos;
		result=2;
	}
	u=volume->get_voxel_position(0,0,0) - volume->get_voxel_position(0,0,volume->depth);
	v=volume->get_voxel_position(0,volume->height,volume->depth) - volume->get_voxel_position(0,0,volume->depth);
	cos = (u.cross_product(v).normalize()).dot_product(grid_z);
	if(cos<smaller){
		smaller=cos;
		result=1;
	}
	
	
	return result;
	
}

void Splatter::render(void) {
	/** 
	 * TODO:
	 * 1 Receber arquivo.
	 * 2 Pegar Slice de voxels
	 * 3 Pra cada voxels : PUFF
	 * 3.1 Calcular contribuição pra cada pixel do GRID através do footprint
	 * 3.2 Somar geral
	 * 4 Compor com o proximo slice : PAH!
	 * 5 Pronto :#
	 */
	
	
	//descobrir o mais paralelo
	switch(parallel()){
		case 1:{
			for(unsigned int i=0; i<volume->width; i++){
				for(unsigned int j=0; j<volume->height; j++){
					for(unsigned int k=0; k<volume->depth; k++){
						// u.u
					}
				}
			}
			break;
		}
		case 2:{
			for(unsigned int i=0; i<volume->height; i++){
				for(unsigned int j=0; j<volume->depth; j++){
					for(unsigned int k=0; k<volume->width; k++){
						// u.u
					}
				}
			}
			break;
		}
		case 3:{
			for(unsigned int i=0; i<volume->depth; i++){
				for(unsigned int j=0; j<volume->height; j++){
					for(unsigned int k=0; k<volume->width; k++){
						
						for(unsigned int x=0;x<grid->width;x++){
							for(unsigned int y=0;y<grid->height;y++){
								double footprint=Footprint::calculate(x,y,volume->get_voxel_position(k,j,i).x,volume->get_voxel_position(k,j,i).y);
								//AJEITAR ISSO!!
								Color color( (volume->get_voxel_weight(k,j,i)/2.0) * footprint,
									     (volume->get_voxel_weight(k,j,i)/2.0) * footprint,
									     (volume->get_voxel_weight(k,j,i)/2.0) * footprint,
									     0.5 * footprint );
								accumulator->set_pixel(x,y,color);
							}
						}
						
					}
				}
			}
			break;
		}
		
	}

	
}



}
